package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.group.domain.MetaverseGroup;
import com.ruoyi.group.service.IMetaverseGroupService;
import com.ruoyi.shop.domain.MetaverseShop;
import com.ruoyi.shop.service.IMetaverseShopService;
import com.ruoyi.system.domain.SysUserPost;
import com.ruoyi.system.domain.SysUserRole;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.shopManager.domain.MetaverseShopManager;
import com.ruoyi.shopManager.service.IMetaverseShopManagerService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 店长管理Controller
 * 
 * @author ruoyi
 * @date 2023-10-19
 */
@RestController
@RequestMapping("/metaverse-shopManager/shopManager")
public class MetaverseShopManagerController extends BaseController
{
    @Autowired
    private IMetaverseShopManagerService metaverseShopManagerService;
    @Autowired
    private ISysUserService userService;
    @Autowired
    private IMetaverseGroupService groupService;
    @Autowired
    private IMetaverseShopService shopService;




    /**hot
     *
     * 查询店长管理列表
     */
    @PreAuthorize("@ss.hasPermi('metaverse-shopManager:shopManager:list')")
    @GetMapping("/list")
    public TableDataInfo list(MetaverseShopManager metaverseShopManager)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        Long groupId = user.getHostId();
        if (groupId != null){
            List<MetaverseShop> shopList = shopService.selectMetaverseShopListAop(groupId);
            if (!shopList.isEmpty()){
                List<Long> id = new ArrayList<>();
                for (MetaverseShop shop : shopList){
                    long ids = shop.getShopId();
                    id.add(ids);
                }
                startPage();
//                    List<MetaverseShopManager> list = metaverseShopManagerService.selectMetaverseShopManagerListAOP(shop.getShopId());
                List<MetaverseShopManager> list = metaverseShopManagerService.selectMetaverseShopManagerListAbc(id);
                return getDataTable(list);
            }else {
                return null;
            }

        }
        startPage();
        List<MetaverseShopManager> list = metaverseShopManagerService.selectMetaverseShopManagerList(metaverseShopManager);
        return getDataTable(list);

    }

    /**
     * 导出店长管理列表
     */
    @PreAuthorize("@ss.hasPermi('metaverse-shopManager:shopManager:export')")
    @Log(title = "店长管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MetaverseShopManager metaverseShopManager)
    {
        List<MetaverseShopManager> list = metaverseShopManagerService.selectMetaverseShopManagerList(metaverseShopManager);
        ExcelUtil<MetaverseShopManager> util = new ExcelUtil<MetaverseShopManager>(MetaverseShopManager.class);
        util.exportExcel(response, list, "店长管理数据");


    }

    /**
     * 获取店长管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('metaverse-shopManager:shopManager:query')")
    @GetMapping(value = "/{shopManagerId}")
    public AjaxResult getInfo(@PathVariable("shopManagerId") Long shopManagerId)
    {
        return success(metaverseShopManagerService.selectMetaverseShopManagerByShopManagerId(shopManagerId));
    }

    /**
     * 新增店长管理
     */
    @PreAuthorize("@ss.hasPermi('metaverse-shopManager:shopManager:add')")
    @Log(title = "店长管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody MetaverseShopManager metaverseShopManager)
    {

        List<MetaverseShopManager> shopManagerList = metaverseShopManagerService.selectMetaverseShopManagerByshopManagerAccount(metaverseShopManager.getShopManagerAccount());

        if (shopManagerList.size()>0){
            return error("新增店长失败，"+metaverseShopManager.getShopManagerAccount()+"用户已存在！");
        }

        metaverseShopManager.setCreateBy(getUsername());
        int num = metaverseShopManagerService.insertMetaverseShopManager(metaverseShopManager);
        System.out.println("num = " + num);
        if (num>0){
            SysUser user = new SysUser();
            long dep = 202;
            user.setUserName(metaverseShopManager.getShopManagerAccount());
            user.setPassword(SecurityUtils.encryptPassword(metaverseShopManager.getShopManagerPassword()));
            user.setNickName(metaverseShopManager.getShopManagerName());
            user.setSex(metaverseShopManager.getSex());
            user.setPhonenumber(metaverseShopManager.getPhone());
            user.setDeptId(dep);
            user.setCreateBy(getUsername());
            user.setHostId(metaverseShopManager.getShopManagerId());
            userService.insertUser(user);

            long roleId = 100;
            SysUserRole userRole = new SysUserRole();
            userRole.setUserId(user.getUserId());
            userRole.setRoleId(roleId);
            userService.insertUserAuthRole(userRole);

            long postId = 4;
            SysUserPost userPost = new SysUserPost();
            userPost.setPostId(postId);
            userPost.setUserId(user.getUserId());
            userService.insertUserAuthPost(userPost);

        }
        return toAjax(num);
    }


    /**
     * 修改店长管理
     */
    @PreAuthorize("@ss.hasPermi('metaverse-shopManager:shopManager:edit')")
    @Log(title = "店长管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MetaverseShopManager metaverseShopManager)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        metaverseShopManager.setUpdateBy(user.getUserName());
        return toAjax(metaverseShopManagerService.updateMetaverseShopManager(metaverseShopManager));
    }


    /**
     * 删除店长管理
     */
    @PreAuthorize("@ss.hasPermi('metaverse-shopManager:shopManager:remove')")
    @Log(title = "店长管理", businessType = BusinessType.DELETE)

	@DeleteMapping("/{shopManagerIds}")
    public AjaxResult remove(@PathVariable Long[] shopManagerIds)
    {
        return toAjax(metaverseShopManagerService.deleteMetaverseShopManagerByShopManagerIds(shopManagerIds));
    }

    /**
     * 判断店长账号是否存在
     */
    @PreAuthorize("@ss.hasPermi('metaverse-shopManager:shopManager:query')")
    @GetMapping(value = "/{shopManagerAccount}")
    public AjaxResult getSelInfo(String shopManagerAccount){
        return success(metaverseShopManagerService.selectMetaverseShopManagerByshopManagerAccount(shopManagerAccount));
    }


}
